package top.continew.minithreadpool.ui.techUi;

import javax.swing.*;
import java.awt.*;
import java.awt.geom.RoundRectangle2D;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BlockingQueue;

public class TechTaskQueuePanel extends JPanel {
    private static final int TASK_WIDTH = 60;
    private static final int TASK_HEIGHT = 30;
    private static final int TASK_SPACING = 15;
    private static final int ANIMATION_SPEED = 5;
    private static final int GLOW_SIZE = 10;

    private BlockingQueue<Runnable> queue;
    private List<TaskItem> taskItems = new ArrayList<>();
    private int maxVisibleTasks;

    // 科技风颜色
    private static final Color BACKGROUND = new Color(15, 23, 42); // 深蓝黑
    private static final Color BELT_COLOR = new Color(30, 40, 60);
    private static final Color WHEEL_COLOR = new Color(100, 110, 130);
    private static final Color GLOW_COLOR = new Color(0, 195, 255, 100);

    public TechTaskQueuePanel(BlockingQueue<Runnable> queue) {
        this.queue = queue;
        setBackground(BACKGROUND);
        setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);

        // 计算最大可见任务数
        maxVisibleTasks = getWidth() / (TASK_WIDTH + TASK_SPACING);

        // 绘制传送带背景
        drawConveyorBelt(g2d);

        // 绘制任务
        drawTasks(g2d);
    }

    private void drawConveyorBelt(Graphics2D g2d) {
        int beltHeight = TASK_HEIGHT + 40;
        int beltY = getHeight() / 2 - beltHeight / 2;

        // 绘制传送带主体
        g2d.setColor(BELT_COLOR);
        g2d.fillRect(0, beltY, getWidth(), beltHeight);

        // 绘制传送带滚轮
        g2d.setColor(WHEEL_COLOR);
        int wheelSize = 20;
        int wheelSpacing = 30;
        for (int i = 0; i < getWidth() / wheelSpacing; i++) {
            int wheelX = i * wheelSpacing + (int)(System.currentTimeMillis() / 50 % wheelSpacing);

            // 绘制滚轮阴影
            g2d.setColor(new Color(0, 0, 0, 100));
            g2d.fillOval(wheelX + 2, beltY - wheelSize/2 + 2, wheelSize, wheelSize);
            g2d.fillOval(wheelX + 2, beltY + beltHeight - wheelSize/2 + 2, wheelSize, wheelSize);

            // 绘制滚轮
            g2d.setColor(WHEEL_COLOR);
            g2d.fillOval(wheelX, beltY - wheelSize/2, wheelSize, wheelSize);
            g2d.fillOval(wheelX, beltY + beltHeight - wheelSize/2, wheelSize, wheelSize);

            // 绘制滚轮高光
            g2d.setColor(Color.WHITE);
            g2d.fillOval(wheelX + 5, beltY - wheelSize/2 + 5, 6, 6);
            g2d.fillOval(wheelX + 5, beltY + beltHeight - wheelSize/2 + 5, 6, 6);
        }

        // 绘制传送带边缘
        g2d.setColor(new Color(60, 70, 90));
        g2d.setStroke(new BasicStroke(2));
        g2d.drawLine(0, beltY, getWidth(), beltY);
        g2d.drawLine(0, beltY + beltHeight, getWidth(), beltY + beltHeight);

        // 绘制传送带发光效果
        g2d.setPaint(new GradientPaint(0, beltY, new Color(0, 100, 200, 50),
                0, beltY + beltHeight, new Color(0, 100, 200, 0)));
        g2d.fillRect(0, beltY, getWidth(), 5);
        g2d.fillRect(0, beltY + beltHeight - 5, getWidth(), 5);
    }

    private void drawTasks(Graphics2D g2d) {
        int startX = 20;
        int y = getHeight() / 2 - TASK_HEIGHT / 2;

        // 同步任务列表
        List<Runnable> currentTasks = new ArrayList<>(queue);
        if (currentTasks.size() > maxVisibleTasks) {
            currentTasks = currentTasks.subList(0, maxVisibleTasks);
        }

        // 更新任务项
        if (taskItems.size() != currentTasks.size()) {
            taskItems.clear();
            for (int i = 0; i < currentTasks.size(); i++) {
                taskItems.add(new TaskItem(i));
            }
        }

        // 绘制任务
        for (int i = 0; i < taskItems.size(); i++) {
            TaskItem taskItem = taskItems.get(i);
            int x = startX + i * (TASK_WIDTH + TASK_SPACING);

            // 更新位置（平滑移动）
            if (taskItem.targetX != x) {
                taskItem.targetX = x;
            }

            if (taskItem.currentX < taskItem.targetX) {
                taskItem.currentX = Math.min(taskItem.currentX + ANIMATION_SPEED, taskItem.targetX);
            } else if (taskItem.currentX > taskItem.targetX) {
                taskItem.currentX = Math.max(taskItem.currentX - ANIMATION_SPEED, taskItem.targetX);
            }

            // 绘制任务方块
            drawTask(g2d, (int) taskItem.currentX, y, i);
        }
    }

    private void drawTask(Graphics2D g2d, int x, int y, int taskId) {
        // 绘制发光效果
        g2d.setColor(GLOW_COLOR);
        for (int i = 0; i < GLOW_SIZE; i++) {
            int size = TASK_WIDTH + i * 2;
            int offset = i;
            g2d.fillRoundRect(x - offset, y - offset, size, TASK_HEIGHT + offset * 2, 15, 15);
        }

        // 绘制任务方块
        RoundRectangle2D rect = new RoundRectangle2D.Double(x, y, TASK_WIDTH, TASK_HEIGHT, 10, 10);

        // 渐变填充
        GradientPaint gradient = new GradientPaint(
                x, y, getTaskColor(taskId),
                x + TASK_WIDTH, y + TASK_HEIGHT, getTaskColor(taskId).darker()
        );
        g2d.setPaint(gradient);
        g2d.fill(rect);

        // 绘制边框
        g2d.setColor(Color.WHITE);
        g2d.setStroke(new BasicStroke(1.5f));
        g2d.draw(rect);

        // 绘制高光
        g2d.setColor(new Color(255, 255, 255, 100));
        g2d.fillRoundRect(x + 2, y + 2, TASK_WIDTH / 2, 5, 5, 5);

        // 绘制任务ID
        g2d.setColor(Color.WHITE);
        g2d.setFont(new Font("Consolas", Font.BOLD, 12));
        String text = "T" + taskId;
        FontMetrics fm = g2d.getFontMetrics();
        int textWidth = fm.stringWidth(text);
        int textX = x + (TASK_WIDTH - textWidth) / 2;
        int textY = y + (TASK_HEIGHT - fm.getHeight()) / 2 + fm.getAscent();
        g2d.drawString(text, textX, textY);
    }

    private Color getTaskColor(int taskId) {
        // 使用HSL颜色模型生成不同色调的颜色
        float hue = (taskId * 0.618f) % 1.0f; // 黄金分割分布
        return Color.getHSBColor(hue, 0.8f, 0.9f);
    }

    public void refresh() {
        repaint();
    }

    private static class TaskItem {
        double currentX;
        int targetX;

        TaskItem(int index) {
            currentX = index * (TASK_WIDTH + TASK_SPACING) + 20;
            targetX = (int) currentX;
        }
    }
}
